package com.icangya.DAO.impl;


import com.icangya.DAO.IGainRecordDAO;
import com.icangya.Utils.SQLUtilt;
import com.icangya.domain.GainRecord;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.List;

import static com.icangya.Value.FootBall.FOOT_PAGE_ROW;
import static com.icangya.Value.StaticValue.FootBall;
import static com.icangya.Value.StaticValue.PHONE_PAGE_ROW;


@Repository
public class GainRecordDAOImpl extends BaseDAO<GainRecord> implements IGainRecordDAO {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public GainRecord getGainTime(Timestamp startTime, Timestamp endTime) {

        GainRecord gainRecord=new GainRecord();
        String hql="select sum(gr.gainMoney) ,sum(gr.expendMoney) ,sum(gr.rechargeMoney)  from GainRecord gr  where gr.createTime between   ?  and  ? ";

        Session session=  getHibernateTemplate().getSessionFactory().getCurrentSession();
//        Query query=session.createSQLQuery(hql).addEntity(GainRecord.class);
        Query query=  session.createQuery(hql);
        query.setParameter(0,startTime);
        query.setParameter(1,endTime);
        List<Object[]> list= query.list();

        logger.info("....................."+list.size());
        for(Object[] objs:list){

            logger.info(objs[0]+""+objs[1]+" "+objs[2]);
        }
        Object[] obj=list.get(0);
        if(obj[0]==null)
            return null;

        gainRecord.setGainMoney((Double) obj[0]);
        gainRecord.setExpendMoney((Double) obj[1]);
        gainRecord.setRechargeMoney((Double) obj[2]);
//        if(list.get)

        return gainRecord;
    }

    @Override
    public List<GainRecord> getGainAll(int startPage) {
        String hql="FROM GainRecord ";
        String count="SELECT COUNT(gain) FROM GainRecord  gain";
        Long total = (Long) getHibernateTemplate().find(count).get(0);
        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, startPage, FOOT_PAGE_ROW);
//        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, startPage, 1);
    }
}
